package com.zsr.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zsr.example.config.EmailConfigProperties;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.service.MsmService;
import com.zsr.example.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送短信
     * @param PhoneNumbers
     * @param templateCode
     * @param param
     * @return
     */
    @Override
    public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) {

        if(StringUtils.isEmpty(PhoneNumbers)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("SignName", "智慧教育");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 邮件发送
     * @param toEmail
     * @param code
     */
    @Override
    @Async
    public void sendEmail(String toEmail, String code) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper minehelper = new MimeMessageHelper(message, true);
            minehelper.setFrom(EmailConfigProperties.MAIL_USERNAME);
            minehelper.setTo(toEmail);
            //TODO 可能添加配置表，后台设置项目名 动态修改邮件发送模板
            minehelper.setSubject("智慧教育验证您的邮箱");
            minehelper.setText("来自智慧教育消息：您的验证码是" + code + "</br>"
                    + "请注意本平台绝不会以任何形式询问您的帐户密码和验证码。</br>"
                    + "本邮件由系统自动生成，请勿直接回复本邮件。</br>", true
            );
            Long startTime = System.currentTimeMillis();
            mailSender.send(message);
            Long endTime = System.currentTimeMillis();
            System.out.println("花费时间" + (endTime - startTime) + "ms");
        } catch (MessagingException e) {
            throw new CommonJsonException(ErrorEnum.E_40002);
        }

    }
}